package com.miaostars.apakey.checks;

import com.miaostars.apakey.Apakey;
import com.miaostars.apakey.utils.PlayerData;
import org.bukkit.entity.Player;
import org.bukkit.util.Vector;

public class SpeedChecks {
    
    private final Apakey plugin;
    
    public SpeedChecks(Apakey plugin) {
        this.plugin = plugin;
    }
    
    public boolean checkSpeed(Player player, PlayerData data, Vector from, Vector to) {
        if (!plugin.getConfigManager().getConfig().getBoolean("checks.speed.enabled")) {
            return false;
        }
        
        if (player.hasPermission("apakey.bypass")) {
            return false;
        }
        
        if (player.isFlying() || player.getAllowFlight()) {
            return false;
        }
        
        if (player.getVehicle() != null) {
            return false;
        }
        
        // Check if player is in liquid or climbing
        if (player.isInWater() || player.isInLava() || player.isClimbing()) {
            data.resetSpeedViolations();
            return false;
        }
        
        // Calculate horizontal distance moved
        double deltaX = to.getX() - from.getX();
        double deltaZ = to.getZ() - from.getZ();
        double distance = Math.sqrt(deltaX * deltaX + deltaZ * deltaZ);
        
        // Get player's walk speed (default is 0.2)
        double maxSpeed = player.getWalkSpeed() * 2.5; // Adjusted multiplier
        
        if (distance > maxSpeed) {
            data.incrementSpeedViolations();
            
            if (data.getSpeedViolations() > plugin.getConfigManager().getConfig().getInt("checks.speed.max-violations")) {
                return true;
            }
        } else {
            data.decrementSpeedViolations();
        }
        
        return false;
    }
}
